package net.googlecode.vasetskiy.task5.calculator;


import net.googlecode.vasetskiy.task5.model.Manager;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Sorts input Collection of manager and prints it.
 * @author Vasetskiy Vlad
 * @since 12.04.11
 */
public class Sorted {
    /**
     * Sorts input Collection to personal code and prints report.
     * @param colect Collection of managers
     */
    public void sorted(Collection<Manager> colect) {
        BigDecimal minsalary,maxsalary;
        SalaryComparator salaryComparator = new SalaryComparator();
        IDComparator idComparator = new IDComparator();
        SortedSet<Manager> salarysortedtemp = new TreeSet<Manager>(salaryComparator);
        salarysortedtemp.addAll(colect);
        minsalary=salarysortedtemp.first().getSalary();
        maxsalary=salarysortedtemp.last().getSalary();
        SortedSet<Manager> idsortedtemp = new TreeSet<Manager>(idComparator);
        idsortedtemp.addAll(colect);
        for (Manager x : idsortedtemp) {
            if ((x.getSalary()!=minsalary) && (x.getSalary()!=maxsalary))System.out.println(x);
        }
    }

    /**
     * Sorts input Collection to Last name and prints report.
     * @param tempColect Collection of managers
     */

    public void lastNameSorted(Collection<Manager> tempColect) {
        NameComparator NameComparator = new NameComparator();
        SortedSet<Manager> namesorted = new TreeSet<Manager>(NameComparator);
        namesorted.addAll(tempColect);
        for (Manager x : namesorted) {
            if (x.getAge() <= 45) {
                System.out.println(x);
            }
        }
    }
    /**
     * Sorts input Collection to personal salary and prints report.
     * @param tempColect Collection of managers
     */
    public void salarySorted(Collection<Manager> tempColect) {
        SalaryComparator salaryComparator = new SalaryComparator();
        BigDecimal prevsalary = new BigDecimal(-1);
        SortedSet<Manager> salarysorted = new TreeSet<Manager>(salaryComparator);
        salarysorted.addAll(tempColect);
        for (Manager x : salarysorted) {
            if (x.getSalary().compareTo(prevsalary) != 0) {
                System.out.println(x);
                prevsalary = x.getSalary();
            }
        }
    }

    /**
     * Class comparator to compare managers to salary.
     */
    class SalaryComparator implements Comparator<Manager> {

        public int compare(Manager o1, Manager o2) {
            return (o2.getSalary().compareTo(o1.getSalary()));
        }
    }

    /**
     * Class comparator to compare managers to personal ID.
     */
    class IDComparator implements Comparator<Manager> {

        public int compare(Manager o1, Manager o2) {
            return o1.getPersonID().compareTo(o2.getPersonID());
        }
    }

    /**
     * Class comparator to compare managers to Last name.
     */
    class NameComparator implements Comparator<Manager> {

        public int compare(Manager o1, Manager o2) {
            return o1.getLastName().compareToIgnoreCase(o2.getLastName());
        }
    }

}
